package cn.wolfcode.wolf2w.gateway.filter;

import cn.wolfcode.wolf2w.common.core.domain.R;
import cn.wolfcode.wolf2w.gateway.util.Md5Util;
import com.alibaba.fastjson.JSON;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SignFilter implements GlobalFilter {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取查询参数
        MultiValueMap<String, String> multiValueMap = exchange.getRequest().getQueryParams();
        Map<String,Object> map = new HashMap<>();
        //获取所有的key并进行遍历
        for (String s : multiValueMap.keySet()) {
            if ("sign".equals(s)){
                //相等，跳过，说明传递的参数正确；不相等 ，进行下一步
                continue;
            }
            List<String> list = multiValueMap.get(s);
            StringBuffer stringBuffer = new StringBuffer();
            for (String s1 : list) {
                stringBuffer.append(s1);
            }
            String value = stringBuffer.toString();
            map.put(s,value);
        }
        String sign_server = Md5Util.signatures(map);// 服务端签名
        String sign_client = multiValueMap.get("sign").get(0);

        //为了测试
        if (sign_client == null){
            String timestep = multiValueMap.getFirst("timestep");
            Long requestTime = Long.valueOf(timestep);
            long now = System.currentTimeMillis();
            if ((now - requestTime) > 0.5*60*1000){
                //找到应答对象的header
                HttpHeaders headers = exchange.getResponse().getHeaders();
                //
                headers.setContentType(MediaType.APPLICATION_JSON);
                R<Object> r = R.fail(500, "接口访问超时");
                //注解中使用了restcontroller，所以不用json，只要下面的databuffer
                String s = JSON.toJSONString(r);
                DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(s.getBytes(StandardCharsets.UTF_8));
                return exchange.getResponse().writeWith(Mono.just(dataBuffer));

            }
            return chain.filter(exchange);
        }

        if (!sign_client.equals(sign_server)){
            //参数被窜改
            //找到应答对象的header
            HttpHeaders headers = exchange.getResponse().getHeaders();
            //
            headers.setContentType(MediaType.APPLICATION_JSON);
            R<Object> r = R.fail(500, "参数被篡改");
            //注解中使用了restcontroller，所以不用json，只要下面的databuffer
            String s = JSON.toJSONString(r);
            DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(s.getBytes(StandardCharsets.UTF_8));
            return exchange.getResponse().writeWith(Mono.just(dataBuffer));

        }

        String timestep = multiValueMap.getFirst("timestep");
        Long requestTime = Long.valueOf(timestep);
        long now = System.currentTimeMillis();
        if ((now - requestTime) > 0.5*60*1000){
            //找到应答对象的header
            HttpHeaders headers = exchange.getResponse().getHeaders();
            //
            headers.setContentType(MediaType.APPLICATION_JSON);
            R<Object> r = R.fail(500, "接口访问超时");
            //注解中使用了restcontroller，所以不用json，只要下面的databuffer
            String s = JSON.toJSONString(r);
            DataBuffer dataBuffer = exchange.getResponse().bufferFactory().wrap(s.getBytes(StandardCharsets.UTF_8));
            return exchange.getResponse().writeWith(Mono.just(dataBuffer));

        }
        return chain.filter(exchange);
    }
}
